import QtQuick 1.0
import "../Components"


DialogBase {
    id: _root
    width: parent.width
    height: parent.height
    x: 0
    y: 0

    dialogTitle: "Setting"


    signal closePressed

    // 表示された時の処理
    function viewEvent(){
        loadSetting();
        _root.forceActiveFocus();
    }

    // 設定を反映する
    function applySetting(){
        _globalSetting.updateInterval = _intervalText.text;
        _globalSetting.inReplyCountMax = _inReplyToCountMaxText.text;
         _globalSetting.toFocusTweetButton = _toFocusTweetButtonText.checked;
    }

    // 設定を読み込む
    function loadSetting(){
        _intervalText.text = _globalSetting.updateInterval.toString();
        _inReplyToCountMaxText.text = _globalSetting.inReplyCountMax.toString();
        _toFocusTweetButtonText.checked = _globalSetting.toFocusTweetButton;
    }

    // パラメータチェック
    function checkValues(){
        var ret = true;

        // 更新間隔
        if(_intervalText.text < 30){
            _toast.show("Please set 30 seconds or more at the update interval.")
            ret = false;
        }
        // in reply toの読込み数
        if(_inReplyToCountMaxText.text < 1){
            _toast.show("Please input a positive number.");
            ret = false;
        }

        return ret;
    }

    Keys.onEscapePressed: {
        closePressed();
    }

    Column{
        anchors.top: parent.titleBottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10

        spacing: 10


        // 更新間隔
        Item{
            id: _intervalArea
            width: parent.width
            height: _intervalText.height

            Text {
                id: _intervalLabel
                anchors.left: parent.left
                height: parent.height
                verticalAlignment: Text.AlignVCenter

                text: "Update interval(sec) : "
                color: "#ffffff"
            }

            TextBox {
                id: _intervalText
                width: 100
                height: 20
                anchors.left: _intervalLabel.right
                anchors.top: parent.top
                anchors.leftMargin: 5

                text: "10"
            }

        }

        // in reply to の取得回数
        Item{
            id: _inReplyToCountMaxArea
            width: parent.width
            height: _inReplyToCountMaxText.height

            Text {
                id: _inReplyToCountMaxLabel
                anchors.left: parent.left
                height: parent.height
                verticalAlignment: Text.AlignVCenter

                text: "Max to get \'in reply to\'(times) : "
                color: "#ffffff"
            }

            TextBox {
                id: _inReplyToCountMaxText
                width: 100
                height: 20
                anchors.left: _inReplyToCountMaxLabel.right
                anchors.top: parent.top
                anchors.leftMargin: 5
            }

        }

        // ツイートボタンをフォーカス移動するか
        Item{
            id: _toFocusTweetButtonArea
            width: _toFocusTweetButtonText.width
            height: _toFocusTweetButtonText.height

            CheckBox {
                id: _toFocusTweetButtonText
                anchors.left: parent.left
                anchors.top: parent.top

                text: "To focus the tweet button"
                color: "#ffffff"
            }

        }
    }


    Row {
        id: _areaButton
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter


        spacing: 5

        Button {
            id: _buttonOk
            width: 100
            height: 25

            text: "O K"

            onClicked : {
//                clickOkButton();
                if(checkValues()){
                    applySetting();
                    _root.closePressed();
                }
            }
        }

        Button {
            id: _buttonCancel
            width: 100
            height: 25

            text: "Cancel"

            onClicked : {
                _root.closePressed();
            }
        }
    }

}
